import 'package:flustars/flustars.dart';
import 'package:flutter/material.dart';
import 'package:flutter_rider/generated/assets.dart';
import 'package:flutter_rider/main/widgets/ext/u_layout.dart';
import 'package:flutter_rider/main/widgets/ext/u_text.dart';
import 'package:flutter_rider/main/widgets/ui_constant.dart';
import 'package:flutter_rider/module/mine/provider/number_car_provider.dart';
import 'package:flutter_rider/routers/fluro_navigator.dart';
import 'package:flutter_rider/utils/log.dart';
import 'package:flutter_rider/utils/screen_fit.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:flutter_svg/flutter_svg.dart';

class CarKeyboard extends ConsumerStatefulWidget {
  const CarKeyboard({Key? key}) : super(key: key);

  @override
  ConsumerState<CarKeyboard> createState() => _CarKeyboardState();
}

class _CarKeyboardState extends ConsumerState<CarKeyboard> {
  final _provinceEnableList = ["领", "警", "学", "港", "澳"];
  final _numberEnableList = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "0"];
  final _letterEnableList = ["O"];

  final provinceList1 = [
    "京",
    "津",
    "渝",
    "沪",
    "冀",
    "晋",
    "辽",
    "吉",
    "黑",
    "苏",
  ];
  final provinceList2 = [
    "浙",
    "皖",
    "闽",
    "赣",
    "鲁",
    "豫",
    "鄂",
    "湘",
    "粤",
    "琼",
  ];
  final provinceList3 = [
    "川",
    "贵",
    "云",
    "陕",
    "甘",
    "青",
    "蒙",
    "桂",
    "宁",
    "新",
  ];
  final provinceList4 = [
    "藏",
    "使",
    "领",
    "警",
    "学",
    "港",
    "澳",
    "删除",
  ];

  final _numberList1 = [
    "1",
    "2",
    "3",
    "4",
    "5",
    "6",
    "7",
    "8",
    "9",
    "0",
  ];
  final _numberList2 = [
    "Q",
    "W",
    "E",
    "R",
    "T",
    "Y",
    "U",
    "O",
    "P",
  ];
  final _numberList3 = [
    "A",
    "S",
    "D",
    "F",
    "G",
    "H",
    "J",
    "K",
    "L",
  ];
  final _numberList4 = [
    "Z",
    "X",
    "C",
    "V",
    "B",
    "N",
    "M",
    "删除",
  ];

  @override
  Widget build(BuildContext context) {
    var numberCarModel = ref.watch(numberCarProvider);
    switch (numberCarModel.keyboardType) {
      case KeyboardType.province:
        return _provinceKeyboard();
      case KeyboardType.letter:
        return _numberKeyboard(_numberEnableList);
      case KeyboardType.numberLetter:
        return _numberKeyboard(_letterEnableList);
    }
  }

  Widget _provinceKeyboard() {
    var one =
        provinceList1.map((e) => _keyBtn(e, _provinceEnableList)).toList();
    var two =
        provinceList2.map((e) => _keyBtn(e, _provinceEnableList)).toList();
    var three =
        provinceList3.map((e) => _keyBtn(e, _provinceEnableList)).toList();
    var four =
        provinceList4.map((e) => _keyBtn(e, _provinceEnableList)).toList();

    return Container(
      color: UIColor.color_t12,
      child: Column(children: [
        8.hspace,
        Row(mainAxisAlignment: MainAxisAlignment.end, children: [
          InkWell(
            child: UText.get("完成", UIColor.color_primary, 14.rpx),
            onTap: () {
              Log.i("完成");
              NavigatorUtils.goBack(context);
            },
          ),
          16.wspace,
        ]),
        8.hspace,
        Row(mainAxisAlignment: MainAxisAlignment.spaceBetween, children: one)
            .padding(EdgeInsets.only(left: 10.rpx, right: 10.rpx)),
        8.hspace,
        Row(mainAxisAlignment: MainAxisAlignment.spaceBetween, children: two)
            .padding(EdgeInsets.only(left: 10.rpx, right: 10.rpx)),
        8.hspace,
        Row(mainAxisAlignment: MainAxisAlignment.spaceBetween, children: three)
            .padding(EdgeInsets.only(left: 10.rpx, right: 10.rpx)),
        8.hspace,
        Row(mainAxisAlignment: MainAxisAlignment.spaceBetween, children: four)
            .padding(EdgeInsets.only(left: 28.rpx, right: 28.rpx)),
        10.hspace,
        ScreenUtil.getInstance().statusBarHeight.hspace
      ]),
    ).intrinsicHeight;
  }

  Widget _numberKeyboard(List enableList) {
    var one = _numberList1.map((e) => _keyBtn(e, enableList)).toList();
    var two = _numberList2.map((e) => _keyBtn(e, enableList)).toList();
    var three = _numberList3.map((e) => _keyBtn(e, enableList)).toList();
    var four = _numberList4.map((e) => _keyBtn(e, enableList)).toList();

    return Container(
      color: UIColor.color_t12,
      child: Column(children: [
        8.hspace,
        Row(mainAxisAlignment: MainAxisAlignment.end, children: [
          InkWell(
            child: UText.get("完成", UIColor.color_primary, 14.rpx),
            onTap: () {
              Log.i("完成");
              NavigatorUtils.goBack(context);
            },
          ),
          16.wspace,
        ]),
        8.hspace,
        Row(mainAxisAlignment: MainAxisAlignment.spaceBetween, children: one)
            .padding(EdgeInsets.only(left: 10.rpx, right: 10.rpx)),
        8.hspace,
        Row(mainAxisAlignment: MainAxisAlignment.spaceBetween, children: two)
            .padding(EdgeInsets.only(left: 28.rpx, right: 28.rpx)),
        8.hspace,
        Row(mainAxisAlignment: MainAxisAlignment.spaceBetween, children: three)
            .padding(EdgeInsets.only(left: 28.rpx, right: 28.rpx)),
        8.hspace,
        Row(mainAxisAlignment: MainAxisAlignment.spaceBetween, children: four)
            .padding(EdgeInsets.only(left: 28.rpx, right: 28.rpx)),
        10.hspace,
        ScreenUtil.getInstance().statusBarHeight.hspace
      ]),
    ).intrinsicHeight;
  }

  Widget _keyBtn(String value, List enableList) {
    var isEnable = !enableList.contains(value);
    var isDelete = value == "删除";

    if (isDelete) {
      return Container(
              decoration: BoxDecoration(
                  color: UIColor.color_content,
                  borderRadius: BorderRadius.circular(4.rpx)),
              padding: EdgeInsets.only(
                  left: 20.rpx, right: 20.rpx, top: 8.rpx, bottom: 8.rpx),
              child: SvgPicture.asset(Assets.imageKeyDelete,
                  width: 24.rpx, height: 24.rpx))
          .onTap(() {
        ref.read(numberCarProvider.notifier).delete();
      });
    }

    return InkWell(
        onTap: isEnable
            ? () {
                ref.read(numberCarProvider.notifier).addVaule(value);
              }
            : null,
        child: Container(
            decoration: BoxDecoration(
                color: UIColor.color_content,
                borderRadius: BorderRadius.circular(4.rpx)),
            padding: EdgeInsets.all(8.rpx),
            child: UText.get(
                value,
                isEnable ? UIColor.color_t16 : Colors.black.withOpacity(0.26),
                16.rpx)));
  }
}
